package com.wn.demo04;

import com.wn.demo03.Person;

import java.util.Arrays;
import java.util.DoubleSummaryStatistics;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yellow Docter
 * @date 2024 -06-17
 * @desc
 *   收集操作
 *      stream 流转成 另外一种方式
 *      collect  流的收集的方法
 *        传入 Collectors  的静态方法据可以
 *           toList();  stream 转成 List
 *           toSet();  stream 转成 List
 *           counting    统计
 *           summingInt  求和
 */
public class Demo03 {

    static List<Person> persons = Arrays.asList(
            new Person("马云", 48, 2000d),
            new Person("马化腾", 50, 3000d),
            new Person("雷军", 46, 4000d),
            new Person("刘强东", 47, 5000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张朝阳", 50, 6000d),
            new Person("张一鸣", 50, 7000d),
            new Person("张一鸣", 50, 7000d)
    );


    public static void main(String[] args) {

        //收集成 集合
        List<Person> collect = persons.stream()
                .distinct()
                .collect(Collectors.toList());
        System.out.println("collect = " + collect);

        System.out.println("*********************");
        List<String> c1 = persons.stream()
                .distinct()
                .map(e -> e.getName())
                .collect(Collectors.toList());
        System.out.println("c1 = " + c1);


        Long collect1 = persons.stream()
                .distinct()
                .collect(Collectors.counting());
        System.out.println("collect1 = " + collect1);

        //求和操作
        DoubleSummaryStatistics collect2 = persons.stream()
                .distinct()
                .filter(e -> e.getSalary() > 5000)
                .map(e -> e.getSalary())
                .collect(Collectors.summarizingDouble(x -> x));
        double sum = collect2.getSum();
        System.out.println("sum = " + sum);

        //求平均值的收集操作
        Double collect3 = persons.stream()
                .distinct()
                .filter(e -> e.getSalary() > 5000)
                .map(e -> e.getSalary())
                .collect(Collectors.averagingDouble(x -> x));
        System.out.println("collect3 = " + collect3);
    }
}